/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.project.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.project.entity.ProjectInfo;
import com.jeeplus.modules.project.mapper.ProjectInfoMapper;
import com.jeeplus.modules.project.entity.ProjectMember;
import com.jeeplus.modules.project.mapper.ProjectMemberMapper;

/**
 * 项目信息Service
 * @author 卢经纬
 * @version 2019-04-10
 */
@Service
@Transactional(readOnly = true)
public class ProjectInfoService extends CrudService<ProjectInfoMapper, ProjectInfo> {

	@Autowired
	private ProjectMemberMapper projectMemberDao;

	public ProjectInfo get(String id) {
		ProjectInfo projectInfo = super.get(id);
		projectInfo.setProjectMemberList(projectMemberDao.findList(new ProjectMember(projectInfo)));
		return projectInfo;
	}

	public List<ProjectInfo> findList(ProjectInfo projectInfo,boolean permission) {
		if (permission) {
			dataRuleFilter(projectInfo);
		}
		return super.findList(projectInfo);
	}

	public Page<ProjectInfo> findPage(Page<ProjectInfo> page, ProjectInfo projectInfo,boolean permission) {
		if (permission) {
			dataRuleFilter(projectInfo);
			// 设置分页参数
		}
		projectInfo.setPage(page);
		// 执行分页查询
		return super.findPage(page, projectInfo);
	}

	@Transactional(readOnly = false)
	public void save(ProjectInfo projectInfo) {
		super.save(projectInfo);
		for (ProjectMember projectMember : projectInfo.getProjectMemberList()){
			if (projectMember.getId() == null){
				continue;
			}
			if (ProjectMember.DEL_FLAG_NORMAL.equals(projectMember.getDelFlag())){
				if (StringUtils.isBlank(projectMember.getId())){
					projectMember.setProjectInfo(projectInfo);
					projectMember.preInsert();
					projectMemberDao.insert(projectMember);
				}else{
					projectMember.preUpdate();
					projectMemberDao.update(projectMember);
				}
			}else{
				projectMemberDao.delete(projectMember);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(ProjectInfo projectInfo) {
		super.delete(projectInfo);
		projectMemberDao.delete(new ProjectMember(projectInfo));
	}

}